package cn.walls1717.wallsmall.geteway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 跨域配置
 *
 * @author walls1717
 * @since 2022/10/22 16:32
 **/
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        // 允许携带cookie跨域
        config.setAllowCredentials(true);
        // 设置请求头
        config.addAllowedHeader("*");
        // 设置请求方法
        config.addAllowedMethod("*");
        // 设置请求源
        config.addAllowedOrigin("*");
        // 设置允许的请求路径，并注册配置
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}
